package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hctforgreen.greenservice.CustomGalleryActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.SubmitFeedBackActivityV3;
import com.hctforgreen.greenservice.model.PhotoEntity;
import com.hctforgreen.greenservice.ui.widget.GridViewIsChildOfScrollView;
import com.hctforgreen.greenservice.utils.DecodeBitmapUtil;


public class SinglePicListAdapter extends BaseAdapter implements
		OnItemClickListener {
	private Activity mActivity;
	private View mConvertView;
	private GridViewIsChildOfScrollView mGridView;

	/**
	 * 图片在SDCard的路径
	 */
	private List<PhotoEntity> mEntities;

	private SubmitFeedBackActivityV3 mSelf;
	
	public SinglePicListAdapter(GridViewIsChildOfScrollView gridView,
			Activity activity, View convertView, List<PhotoEntity> entities
			, SubmitFeedBackActivityV3 self) {

		mGridView = gridView;
		mActivity = activity;
		mConvertView = convertView;
		mEntities = entities;
		mSelf = self;
		initPhotoList();
		mGridView.setAdapter(this);
		mGridView.setOnItemClickListener(this);
	}

	private void initPhotoList(){
		if(mEntities == null || mEntities.size() == 0){
			PhotoEntity entity = new PhotoEntity();
			mEntities.add(entity);
		}
	}
	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		final PhotoEntity entity = (PhotoEntity) getItem(position);
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_single_pic_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (ViewCache) convertView.getTag();
		}
		if(!entity.path.equals("")){//展示图片
			DecodeBitmapUtil bitmapUtil = new DecodeBitmapUtil();
			viewCache.getAddIv().setImageBitmap(bitmapUtil.decodeBitmap(entity.path));
			viewCache.getDeleteIv().setVisibility(View.VISIBLE);
			
		}else if(entity.path.equals("")){//展示增加按钮
			viewCache.getAddIv().setImageResource(R.drawable.add_pic_btn_u);
			viewCache.getDeleteIv().setVisibility(View.GONE);
		}
		viewCache.getAddIv().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				add(entity, position);
				
			}
		});
		viewCache.getDeleteIv().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				delete(entity, position);
			}
		});
		return convertView;
	}
	private void delete(PhotoEntity entity, int position){
		if(entity.path.equals("")){
			return;
		}
		if(position != 0){
			mEntities.remove(position);
		}else if(position == 0 && mEntities.size() == 1){
			entity.path = "";
			mEntities.set(0, entity);
		}else if(position == 0 && mEntities.size() > 0){
			mEntities.remove(position);
		}
		
		boolean bCanAdd = true;
		for(PhotoEntity photoEntity : mEntities){
			if(photoEntity.path.equals("")){
				bCanAdd = false;
			}
		}
		if(bCanAdd){
			mEntities.add(new PhotoEntity());
		}
		notifyDataSetChanged();
	}
	/**
	 * 相册
	 */
	private void add(PhotoEntity entity, int position) {
		mSelf.mPosition = position;
		try {
//			Intent intent = getPhotoPickIntent();
//			mActivity.startActivityForResult(intent, mSelf.PHOTO_PICKED_WITH_DATA);
//			Intent i = new Intent("luminous.ACTION_MULTIPLE_PICK");
			
			Intent i = new Intent(mSelf,CustomGalleryActivity.class);
			if(position+1<mEntities.size()||position+1==6){//当选中更改中间图片或者最后第6张时，只能单选
				i.putExtra("galleryType", "luminous.ACTION_PICK");
				i.putExtra("picListCount", mEntities.size());
			}else{//否则打开多选
				i.putExtra("galleryType", "luminous.ACTION_MULTIPLE_PICK");
				i.putExtra("picListCount", mEntities.size());
			}
			mActivity.startActivityForResult(i, mSelf.PHOTO_PICKED_WITH_DATA);
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	public static Intent getPhotoPickIntent() {

		//Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("image/*");
		return intent;
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

	class ViewCache {
		private View baseView;

		private ImageView addIv;
		private ImageView deleteIv;
		
		public ViewCache(View view) {
			baseView = view;
		}

		public ImageView getAddIv() {
			if (addIv == null) {
				addIv = (ImageView) baseView
						.findViewById(R.id.iv_add);
			}
			return addIv;
		}
		public ImageView getDeleteIv() {
			if (deleteIv == null) {
				deleteIv = (ImageView) baseView
						.findViewById(R.id.iv_delete);
			}
			return deleteIv;
		}
	}
}
